#!/bin/bash

help() {
  echo "Usage:"
  echo "  zmicro plugin remove <plugin_name>"
  echo ""
}

core() {
  if [[ $# -eq 0 ]]; then
    help
    exit 0
  fi

  while [[ $# -gt 0 ]]; do
    local key=$1
    local value=$2
    case $key in
    -h | --help)
      help
      shift # past key
      exit 0
      ;;
    *)
      # echo "ERROR: unknown parameter \"$key\""
      # help
      # exit 1
      break
      ;;
    esac
  done

  local plugin_name=$1

  log::debug "[$(timestamp)][plugin] remove $plugin_name ..."

  local plugin_path=$ZMICRO_PLUGINS_PATH/$plugin_name
  if [ ! -d $plugin_path ]; then
    log "[$(timestamp)][plugin] already removed"
    exit 0
  fi

  log::info "[$(timestamp)][plugin] removing $(color::red $plugin_name) ..."

  # unregister
  plugin::unregister $plugin_name
  # remove code
  rm -rf $plugin_path

  log::success "[$(timestamp)][plugin] remove success $plugin_name"
}

run() {
  core $@
}

run $@
